package domain.facade;

import java.util.HashMap;

import dao.UsersDao;
import dao.pojo.Types;
import dao.pojo.Users;
import domain.UserFacade;

/**
 * Created by IntelliJ IDEA.
 * User: winstars
 * Date: 2007-3-1
 * Time: 18:33:53
 * To change this template use File | Settings | File Templates.
 */
class UserService implements UserFacade {

    private UsersDao usersDao;
    private HashMap errors;
    
    private static final Integer COMMON_TYPE_ID=1;
    private static final Integer SUPER_TYPE_ID=2;
    
    public static final Integer USER_NAME_NOT_VALID=3;
    
    public UserService(){//TODO 用户注册返回的错误类型判别
    	errors=new HashMap();
    	errors.put(USER_NAME_NOT_VALID,"用户名不合法");
    }

    protected UsersDao getUsersDao() {
        return usersDao;
    }

    public void setUsersDao(UsersDao usersDao) {
        this.usersDao = usersDao;
    }

    public Users login(Users user)//登录
    {
        Users newUser = usersDao.getUsersByUserName(user);//按名取用户
        if (newUser != null) {//用户存在
            if (newUser.getPassword().equals(user.getPassword())) 
            	 return newUser;  //密码一致
        }
		return newUser;

    }

    public boolean addUsers(Users user) {
        // TODO Auto-generated method stub

        Users users=usersDao.getUsersByUserName(user);
        if(users==null){//判断是否存在同名用户
        	if( valid( user.getUserName() ) && valid( user.getPassword() ) ){//如果用户名和密码不违例
        		if(user.getType()==null){//赋予普通用户权限
        			Types type=new Types();
        			type.setId(COMMON_TYPE_ID);
        			user.setType(type);
        		}
        		usersDao.addUsers(user);//新增用户
        		return true;//添加成功
        	}
        }
        return false;//添加失败
    }
    
    public boolean valid(String s){
    	if(s != null && !s.contains(" ") && !s.equals("")){//'为空' '包含空字符' '本身为空字符串'  都为违例情况
    		return true;
    	}
    	return false;
    }

    public boolean deleteUsers(Users user) {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean updateUsers(Users user) {
        // TODO Auto-generated method stub
        return false;
    }

}
